// components/order/order-list-item/index.js
const componentBehaviors = require('../../../../behaviors/component');
import { COMPONENT_OPTIONS } from '../../../../constants/common';
import { COMMON_PAGE, PACKAGE_ACTIVITY_PAGE } from '../../../../constants/route';
import { ORDER_TYPE } from '../../../../constants/order';
import { handleOrderStatus, handleAfterSaleOrderStatus } from '../../../biztools/order';
import { navTo } from '../../../../common/env';
import { formatDate } from '../../../../common/utils';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    /**
     * 组件的属性列表
     */
    properties: {
        // 订单信息
        orderInfo: {
            type: Object,
            value: {},
        },
        // 是否是售后单
        isAfterSale: {
            type: Boolean,
            value: false,
        },
        isExpand: {
            type: Boolean,
            value: false,
        },
    },
    observers: {
        'orderInfo, isAfterSale': function(info, flag) {
            const orderStatusInfo = flag ? handleAfterSaleOrderStatus(info) : handleOrderStatus(info);
            this.setData({
                orderStatusInfo,
                orderTime: formatDate(flag ? info.applied : info.ordered),
                isHaveDivider: this.dealIsHaveDivider(flag, orderStatusInfo),
            });
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        ORDER_TYPE,
        orderStatusInfo: handleOrderStatus({}),
        orderTime: '',
        isHaveDivider: false,
    },

    /**
     * 组件的方法列表
     */
    methods: {
        dealIsHaveDivider: function (isAfterSale, orderStatusInfo) {
            if (isAfterSale) {
                return orderStatusInfo.isHaveCancel;
            } else {
                return orderStatusInfo.isHaveListCancel || orderStatusInfo.isHavePay || orderStatusInfo.isHaveContactBrand || orderStatusInfo.isHaveConfirm;
            }
        },
        goToOrderDetail: function () {
            this.triggerEvent('detail', this.data.orderInfo);
        },
        // 立即支付
        payAction: function() {
            this.triggerEvent('pay', this.data.orderInfo);
        },
        // 取消订单
        cancelAction: function() {
            const { isAfterSale } = this.data;
            if (isAfterSale) {
                this.triggerEvent('cancelAfter', this.data.orderInfo);
            } else {
                this.triggerEvent('cancel', this.data.orderInfo);
            }
        },
        // 确认收货
        confirmExpressAction: function() {
            this.triggerEvent('express', this.data.orderInfo);
        },
        // 确认提货
        confirmSelfPickAction: function() {
            this.triggerEvent('pickup', this.data.orderInfo);
        },
        // 取货码
        pickUpCodeAction: function () {},
        // 联系商家
        contactAction: function() {
            this.triggerEvent('contact', this.data.orderInfo);
        },
        goToActivity() {
            const { activityId, orderType } = this.data.orderInfo;
            if (orderType === ORDER_TYPE.SALE) {
                navTo(PACKAGE_ACTIVITY_PAGE['detail'], {
                    activityId
                });
            }
        },
        goToOwner() {
            const { brandId, storeId } = this.data.orderInfo;
            if (storeId && storeId > 0) {
                navTo(COMMON_PAGE.shareStore, {
                    storeId
                });
            } else {
                navTo(COMMON_PAGE.shareBrand, {
                    brandId
                });
            }
        },
    }
})
